/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.tool;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.activerecord.generator.Generator;

import top.wardyan.base.action.BaseShell;
import top.wardyan.base.config.Const;
import top.wardyan.base.datasource.BaseSQLServerDatasource;
import top.wardyan.base.util.SqlserverMetaBuilder;
import top.wardyan.base.util.swt.Msg;
import top.wardyan.po.WButton;
import top.wardyan.po.WComposite;
import top.wardyan.po.WLabel;
import top.wardyan.po.WLabelTitle;
import top.wardyan.po.WText;
import top.wardyan.po.WTextInput;

/**
 * 数据库实体生成器
 * @author ward 2019年2月24日
 */
public class BeanGenerator extends BaseShell<BeanGenerator>{
	
	private static final Integer LINEHEIGHT = 27;
	private static final Integer MARGIN = 20;
	private static final Integer TEXTLINEHEIGHT = LINEHEIGHT * 8 / 10;
	
	protected WLabelTitle	lblTop;
	protected WLabel		lblDatabase;
	protected WComposite	comDatabase;
	
	protected WTextInput	txtAddress;
	protected WTextInput	txtPort;
	protected WTextInput	txtDBName;
	protected WTextInput	txtUsername;
	protected WTextInput	txtPassword;
	
	protected Button		btnOK;
	
	protected WComposite	comRadios;
	protected WLabel		lblRadios;
	protected WButton		chkSQLServer;
	protected WButton		chkMysql;
	protected WButton		chkSQLite;
	
	protected WComposite	comChoices;
	protected WLabel		lblChoices;
	/** 移除前缀 **/
	protected WTextInput	txtPrefixRemoving;
	
	protected WComposite	comPackages;
	protected WLabel		lblPackages;
	protected WTextInput	txtModelPackage;
	protected WLabel		lblLocation;
	protected WText		txtOutputDir;
	
	
	//protected 
	
	public BeanGenerator() {
		super();
		//this.init();
		this.createContent();
	}
	
	protected void createContent() {
		lblTop = new WLabelTitle(this, this.getTitle());
		lblDatabase = new WLabel(this, new Rectangle(MARGIN,lblTop.getBottom().y + 5, this.getSize().x - MARGIN * 2, LINEHEIGHT * 15 / 10), "数据库连接");
		comDatabase = new WComposite(this, new Rectangle(MARGIN, lblDatabase.getBottom().y + 5, this.getSize().x - MARGIN * 2, LINEHEIGHT * 3));
		
		txtAddress = new WTextInput(	comDatabase, 
									new Rectangle(MARGIN, 0, LINEHEIGHT * 10, LINEHEIGHT), 
									"地址", 
									50);
		txtPort = new WTextInput(		comDatabase, 
									new Rectangle(txtAddress.getBottom().x + 5, txtAddress.getLocation().y, LINEHEIGHT * 6, LINEHEIGHT),
									"端口",
									6,
									txtAddress.getLabelSize());
		txtDBName = new WTextInput(	comDatabase, 
									new Rectangle(txtPort.getBottom().x + 5, txtAddress.getLocation().y, LINEHEIGHT * 8, LINEHEIGHT),
									"数据库",
									50,
									txtAddress.getLabelSize());
		txtUsername = new WTextInput(	comDatabase, 
									new Rectangle(txtDBName.getBottom().x + 5, txtAddress.getLocation().y, LINEHEIGHT * 8, LINEHEIGHT),
									"用户名",
									50,
									txtAddress.getLabelSize());
		txtPassword = new WTextInput(	comDatabase, 
									new Rectangle(txtUsername.getBottom().x + 5, txtAddress.getLocation().y, LINEHEIGHT * 8, LINEHEIGHT),
									"密码",
									50,
									txtAddress.getLabelSize());
		
		comRadios = new WComposite(this, new Rectangle(MARGIN, comDatabase.getBottom().y + 5, this.getSize().x - MARGIN * 2, LINEHEIGHT * 3));
		lblRadios = new WLabel(comRadios, new Rectangle(0, 0, this.getSize().x - MARGIN * 2, LINEHEIGHT * 15 / 10), "数据库类型");
		
		chkSQLServer = new WButton(	comRadios, 
									SWT.RADIO, 
									new Rectangle(MARGIN, lblRadios.getBottom().y + 5, LINEHEIGHT * 4, LINEHEIGHT),
									"SQL SERVER",
									null,
									true);
		chkMysql = new WButton(	comRadios, 
									SWT.RADIO, 
									new Rectangle(chkSQLServer.getBottom().x + 5, lblRadios.getBottom().y + 5, LINEHEIGHT * 4, LINEHEIGHT),
									"MYSQL",
									null,
									false);
		chkMysql.setEnabled(false);
		chkSQLite = new WButton(	comRadios, 
									SWT.RADIO, 
									new Rectangle(chkMysql.getBottom().x + 5, lblRadios.getBottom().y + 5, LINEHEIGHT * 4, LINEHEIGHT),
									"SQLITE",
									null,
									false);
		chkSQLite.setEnabled(false);
		
		
		comChoices = new WComposite(this, new Rectangle(MARGIN, comRadios.getBottom().y + 5, this.getSize().x - MARGIN * 2, LINEHEIGHT * 3));
		lblChoices = new WLabel(comChoices, new Rectangle(MARGIN, 0, this.getSize().x - MARGIN * 2, LINEHEIGHT * 15 / 10), "选项");
		txtPrefixRemoving = new WTextInput(	comChoices, 
											new Rectangle(MARGIN, lblChoices.getBottom().y + 5, LINEHEIGHT * 10, LINEHEIGHT),
											"去前缀",
											50,
											txtAddress.getLabelSize());

		
		
		
		
		comPackages = new WComposite(this, new Rectangle(MARGIN, comChoices.getBottom().y + 5, this.getSize().x - MARGIN * 2, LINEHEIGHT * 3));
		lblPackages = new WLabel(comPackages, new Rectangle(MARGIN, 0, this.getSize().x - MARGIN * 2, LINEHEIGHT * 15 / 10), "实体类设定");
		txtModelPackage = new WTextInput(	comPackages, 
									new Rectangle(MARGIN, lblPackages.getBottom().y + 5, LINEHEIGHT * 12, LINEHEIGHT),
									"包名",
									250,
									txtAddress.getLabelSize());
		
		lblLocation = new WLabel(	comPackages, 
								new Rectangle(txtModelPackage.getBottom().x + 15, lblPackages.getBottom().y + 5, LINEHEIGHT * 4, LINEHEIGHT),
								Const.FONT_H7,
								"输出位置",
								SWT.RIGHT);
		txtOutputDir = new WText(	comPackages,
									new Rectangle(lblLocation.getBottom().x + 5, lblPackages.getBottom().y + 5, LINEHEIGHT * 14, LINEHEIGHT), 
									"", 
									Const.FONT_H8);
		txtOutputDir.setEditable(false);
		Button btn = new Button(comPackages, SWT.PUSH);
		btn.setBounds(txtOutputDir.getBottom().x + 2, lblPackages.getBottom().y + 5, LINEHEIGHT * 3, LINEHEIGHT);
		btn.setText("选择");
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog d = new DirectoryDialog(self);
				d.setText("选择存放目录");
				d.setMessage("请选择要将model放到哪个目录:");
				d.setFilterPath("//");
				String path = d.open();
				//System.out.println(path);
				if(path != null)
					txtOutputDir.setText(path);
			}
		});
		
		btnOK = new WButton(	this, 
								SWT.NONE, 
								new Rectangle(MARGIN, comPackages.getBottom().y + 10, LINEHEIGHT * 3, LINEHEIGHT),
								"生成",
								new SelectionAdapter() {
									@Override
									public void widgetSelected(SelectionEvent event) {
										self.build();
									}								
								},
								false);
	}
		
	
	
	protected void build() {
		DataSource ds = new DataSource();
		
		String modelPackageName = txtModelPackage.getText();	// "ward.sim.model";
		String baseModelPackageName = modelPackageName + ".base";		// "ward.sim.model.base";
	    
		// model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
	    String modelOutputDir = txtOutputDir.getText();
		// base model 文件保存路径
	    String baseModelOutputDir = modelOutputDir + "/base";;
	    // model 所使用的包名 (MappingKit 默认使用的包名)
	    
	    

	    // 创建生成器
	    
	    
	    Generator generator = new Generator(ds.getDp().getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
	    // 设置数据库方言
	    //generator.setDialect(new MysqlDialect());
	    generator.setDialect(new SqlServerDialect());
	    // 添加不需要生成的表名
	    //generator.addExcludedTable("adv");
	    // 设置是否在 Model 中生成 dao 对象
	    generator.setGenerateDaoInModel(true);
	    // 设置是否生成链式 setter 方法
	    //generator.setGenerateChainSetter(true);
        // 添加不需要生成的表名
	    //generator.addExcludedTable("adv");
        // 设置是否在 Model 中生成 dao 对象
	    generator.setGenerateDaoInModel(true);
        // 设置是否生成链式 setter 方法
	    generator.setGenerateChainSetter(true);
	    // 设置是否生成字典文件
	    generator.setGenerateDataDictionary(true);
	    // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user"，移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
	    //generator.setRemovedTableNamePrefixes("wt_");
	    if(txtPrefixRemoving.getText().length() > 0) {
	    	generator.setRemovedTableNamePrefixes(txtPrefixRemoving.getText());
	    }
	    // 这一行先留着
	    generator.setMetaBuilder(new SqlserverMetaBuilder(ds.getDp().getDataSource(),false));
	    // 生成
	    try {
	    	generator.generate();
	    	Msg.showMessage("结果", "操作结束, 请检查结果");
	    }catch(Exception e) {
	    	//System.out.println(e);
	    	e.printStackTrace();
	    	Msg.showMessage("结果", "操作失败:" + e.getMessage());
	    }
	}
	
	class DataSource extends BaseSQLServerDatasource{

		protected DataSource() {
			this(txtAddress.getText(), Integer.valueOf(txtPort.getText()), txtDBName.getText(), txtUsername.getText(), txtPassword.getText(), txtDBName.getText());
		}
		
		protected DataSource(String address, Integer port, String dbName, String user, String password, String arpName)
				throws RuntimeException {
			super(address, port, dbName, user, password, arpName);
		}

		/* (non-Javadoc)
		 * @see top.wardyan.base.datasource.AbstractDatasource#mapping()
		 */
		@Override
		protected void mapping() {
		}
		
	}
	
	
	
	@Override
	protected String getTitle() {
		return "实体生成器";
	}
	
	public static void main(String[] args) {
		new BeanGenerator().go();
	}

	
}
